package com.nnnu.wsnackshop.controller.admin;


import com.nnnu.wsnackshop.constant.MessageConstants;
import com.nnnu.wsnackshop.pojo.dto.AddStoreDTO;
import com.nnnu.wsnackshop.pojo.dto.StorePageDTO;
import com.nnnu.wsnackshop.pojo.dto.UpdateStoreDTO;
import com.nnnu.wsnackshop.pojo.vo.StoreVO;
import com.nnnu.wsnackshop.result.Result;
import com.nnnu.wsnackshop.service.IStoresService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 门店表 前端控制器
 * </p>
 *
 * @author zk
 * @since 2025-05-19
 */
@RestController("adminStoresController")
@RequestMapping("/api/admin/stores")
@Slf4j
@Validated
@Tag(name = "管理端门店接口")
@RequiredArgsConstructor
public class StoresController {

    private final IStoresService storeService;

    @Operation(summary = "分页查询门店", description = "仅 superAdmin 或 admin 且拥有 store:read 权限可访问")
    @GetMapping
    @RequiresRoles(value = {"superAdmin", "admin"}, logical = Logical.OR)
    @RequiresPermissions("store:read")
    public Result<?> pageStores(@Valid @ParameterObject StorePageDTO dto) {
        log.info("管理员分页查询门店：{}", dto);
        return Result.success(storeService.pageStores(dto), MessageConstants.SELECT_OK);
    }

    @Operation(summary = "获取门店详情", description = "仅 superAdmin 或 admin 且拥有 store:read 权限可访问")
    @GetMapping("/{id}")
    @RequiresRoles(value = {"superAdmin", "admin"}, logical = Logical.OR)
    @RequiresPermissions("store:read")
    public Result<StoreVO> getStore(@PathVariable @Parameter(description = "门店ID", in = ParameterIn.PATH) Long id) {
        return Result.success(storeService.getStoreDetail(id), MessageConstants.SELECT_OK);
    }

    @Operation(summary = "创建门店", description = "仅 superAdmin 且拥有 store:create 权限可访问")
    @PostMapping
    @RequiresRoles("superAdmin")
    @RequiresPermissions("store:create")
    @CacheEvict(cacheNames = {"userStorePages", "userStoreDetail", "userStoreAll"}, allEntries = true)
    public Result<Void> createStore(@Valid @RequestBody @Parameter(description = "门店信息") AddStoreDTO dto) {
        log.info("创建门店：{}", dto);
        storeService.createStore(dto);
        return Result.success(MessageConstants.ADD_OK);
    }

    @Operation(summary = "更新门店", description = "仅 superAdmin 且拥有 store:update 权限可访问")
    @PutMapping
    @RequiresRoles("superAdmin")
    @RequiresPermissions("store:update")
    @CacheEvict(cacheNames = {"userStorePages", "userStoreDetail", "userStoreAll"}, allEntries = true)
    public Result<Void> updateStore(@Valid @RequestBody @Parameter(description = "门店信息") UpdateStoreDTO dto) {
        log.info("更新门店：{}", dto);
        storeService.updateStore(dto);
        return Result.success(MessageConstants.OPERATION_SUCCESSFUL);
    }

    @Operation(summary = "批量删除门店", description = "仅 superAdmin 且拥有 store:delete 权限可访问")
    @DeleteMapping
    @RequiresRoles("superAdmin")
    @RequiresPermissions("store:delete")
    @CacheEvict(cacheNames = {"userStorePages", "userStoreDetail", "userStoreAll"}, allEntries = true)
    public Result<Void> deleteStores(@Valid @RequestParam @Parameter(description = "门店ID列表", in = ParameterIn.QUERY) List<Long> ids) {
        log.info("批量删除门店：{}", ids);
        storeService.deleteStores(ids);
        return Result.success(MessageConstants.OPERATION_SUCCESSFUL);
    }

    @Operation(summary = "开启/关闭门店", description = "仅 superAdmin 且拥有 store:toggle 权限可访问")
    @PatchMapping("/{id}/{status}")
    @RequiresRoles("superAdmin")
    @RequiresPermissions("store:toggle")
    @CacheEvict(cacheNames = {"userStorePages", "userStoreDetail", "userStoreAll"}, allEntries = true)
    public Result<Void> toggleStore(@PathVariable @Parameter(description = "门店ID", in = ParameterIn.PATH) Long id, @PathVariable @Parameter(description = "状态", in = ParameterIn.PATH) Integer status) {
        log.info("门店{}状态改为{}", id, status);
        storeService.toggleStoreStatus(id, status);
        return Result.success(MessageConstants.OPERATION_SUCCESSFUL);
    }
}
